package com.example.starter.config;

import com.example.starter.properties.StudentProperties;
import com.example.starter.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 *
 * @EnableConfigurationProperties注解告诉Spring Boot 能支持@ConfigurationProperties
 * Created by caorl on 2020/9/21.
 */
@Configuration
@EnableConfigurationProperties(StudentProperties.class)
//@ConditionalOnProperty(prefix = "student", name = "name")
public class StudentConfig {
    @Autowired
    private StudentProperties studentProperties;

    @Primary
    @Bean(name = "student")
    public StudentService studentService(){
        StudentService studentService = new StudentService(studentProperties.getName(), this.studentProperties.getAge());
        System.out.println(studentProperties.toString());
        return studentService;
    }
}